package com.example.beidouapplication;

import android.app.Fragment;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableField;

import com.example.beidouapplication.databinding.FragmentContentParaSetBinding;
import com.example.beidouapplication.viewmodel.Key;
import com.example.beidouapplication.viewmodel.KeyEvent;
import com.jay.easykeyboard.SystemKeyBoardEditText;


public class FragmentParaSet extends Fragment {

    SystemKeyBoardEditText input1;
    SystemKeyBoardEditText input2;
    Button btn;


    static FragmentParaSet instance = new FragmentParaSet();

    FragmentContentParaSetBinding binding;

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {


        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_content_para_set, container, false);
        binding.setData(MainActivity.paraSetData);

        return binding.getRoot();

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        input1 = getActivity().findViewById(R.id.edit_para_set_1);
        input2 =  getActivity().findViewById(R.id.edit_para_set_2);

        input1.setInputType(InputType.TYPE_CLASS_NUMBER| InputType.TYPE_NUMBER_FLAG_DECIMAL |InputType.TYPE_NUMBER_FLAG_SIGNED);
        input2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL );


        input1.setOnKeyboardActionListener(new NextTextWatcher(input2));
        input2.setOnKeyboardActionListener(new EmptyKeyBoardActionListener());

        MainActivity.paraSetData.text1.set("");
        MainActivity.paraSetData.text2.set("");
        btn =  getActivity().findViewById(R.id.btn_para_set);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyEvent keyEvent = new KeyEvent(Key.ENTER, -1);

                ObservableField<String> input = MainActivity.paraSetData.text1;
                String trim = input.get().trim();
                if (trim.length() == 0) {
                    trim = "0";
                    input.set(trim);
                }

                Menu.mntErr = Double.parseDouble(input.get());
                keyEvent.extra.put("mntErr", input.get());

                input = MainActivity.paraSetData.text2;
                 trim = input.get().trim();
                if (trim.length() == 0) {
                    trim = "0";
                    input.set(trim);
                }


                Menu.k = Double.parseDouble(input.get());
                keyEvent.extra.put("k", input.get());
                Menu.keyQueue.add(keyEvent);
            }
        });

    }


}